﻿using Undercover.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Undercover.ViewModel
{
    public class TreasureViewModel : ViewModelBase
    {
        public TreasureViewModel()
            : this(new Treasure())
        {
        }

        public TreasureViewModel(Treasure treasure)
        {
            _treasure = treasure;

            BuyPackageCommand = new RelayCommand<WordPackage>(
                (WordPackage package) =>
                {
                    MessageBox.Show("Now pop a dialog for online purchase: " + package.DisplayName + ", " + package.ProductID);
                    // Go to online shop to buy.
                   
                    //ServiceLocator.Current.GetInstance<INavigationService>()
                    //    .NavigateTo(new Uri(_menuMap[tb.Text], UriKind.Relative));
                }
                );

            ClearUseHistoryCommand = new RelayCommand(
                () =>
                {
                    foreach (var p in Treasure.Global.Value.WordPackages.Where(it => it.Purchased))
                    {
                        var list = p.GetWordPairList();
                        if (list != null)
                        {
                            foreach (var w in list)
                            {
                                w.UseCount = 0;
                            }
                        }
                    }
                }
            );
        }

        public ICommand BuyPackageCommand { get; private set; }
        public ICommand ClearUseHistoryCommand { get; private set; }

        public int UseCount { get { return _treasure.UseCount; } }
        public int TotalCount { get { return _treasure.TotalCount; } }

        public ObservableCollection<WordPackage> WordPackages
        {
            get
            {
                if (_wordPackages == null)
                {
                    _wordPackages = new ObservableCollection<WordPackage>(_treasure.WordPackages);
                }
                return _wordPackages;
            }
        }
        private ObservableCollection<WordPackage> _wordPackages;

        private Undercover.Model.Treasure _treasure;
    }
}
